/*******************************************************************************
 * Copyright (c) 2008 The University of York.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Louis Rose - initial API and implementation
 ******************************************************************************/
package autoz.czt.launching.tabs;

import static autoz.czt.launching.LaunchConfigurationAttributes.MODEL;
import static autoz.czt.launching.LaunchConfigurationAttributes.OUTPUT_FILE_PATH;
import static autoz.czt.launching.LaunchConfigurationAttributes.SOURCE;
import static autoz.czt.launching.LaunchConfigurationAttributes.USE_DEFAULT_TEMPLATES;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import autoz.core.launching.AbstractSourceConfigurationTab;
import autoz.czt.CZTPlugin;

public class SourceConfigurationTab extends AbstractSourceConfigurationTab implements SelectionListener {
	
	protected Text eglOutputFilePath;
	protected Text eglSourceFilePath;
	private Button browseForSourceFile;
	private Button browseForOutputFile;
	private Button useDefaultTemplates;
	
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout( new RowLayout());
		
		new SourceConfigurationTab().createControl(shell);
		
		shell.pack();
		shell.open();
		while( !shell.isDisposed())
		{
			if(!display.readAndDispatch()) 
				display.sleep();
		}
		display.dispose();
	}
	
	@Override
	public void createControl(Composite parent) {
		
		FillLayout parentLayout = new FillLayout();
		parent.setLayout(parentLayout);

		Composite control = new Composite(parent, SWT.NONE);
		setControl(control);
		
		control.setLayout(new GridLayout(1, false));
		
// EGL source file:
		Group eglSourceGroup = new Group(control, SWT.SHADOW_ETCHED_IN);
		eglSourceGroup.setLayout(new GridLayout(1, false));
		eglSourceGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		eglSourceGroup.setText("FTL source file: ");	
		
		Composite sourceFileContainer = new Composite(eglSourceGroup, SWT.NONE);
		sourceFileContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		sourceFileContainer.setLayout(new GridLayout(2, false));

		GridData useDefaultTemplatesData = new GridData(GridData.FILL_HORIZONTAL);
		useDefaultTemplatesData.horizontalIndent = 25;
		useDefaultTemplatesData.horizontalSpan = 2;
		useDefaultTemplates = new Button(sourceFileContainer, SWT.CHECK);
		useDefaultTemplates.setText("Use default templates");
		useDefaultTemplates.setLayoutData(useDefaultTemplatesData);
		useDefaultTemplates.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {
				
				if (useDefaultTemplates.getSelection()){
					eglSourceFilePath.setEnabled(false);
					browseForSourceFile.setEnabled(false);
				}
				else{
					eglSourceFilePath.setEnabled(true);
					browseForSourceFile.setEnabled(true);
				}
				canSave();
				updateLaunchConfigurationDialog();
				
			}

			public void widgetSelected(SelectionEvent e) {
				
				if (useDefaultTemplates.getSelection()){
					eglSourceFilePath.setEnabled(false);
					browseForSourceFile.setEnabled(false);
				}
				else{
					eglSourceFilePath.setEnabled(true);
					browseForSourceFile.setEnabled(true);
				}
				canSave();
				updateLaunchConfigurationDialog();
			}
			
		});
		
		GridData sourceFilePathData = new GridData(GridData.FILL_HORIZONTAL);
		sourceFilePathData.horizontalIndent = 25;
		eglSourceFilePath = new Text(sourceFileContainer, SWT.BORDER);
		eglSourceFilePath.setLayoutData(sourceFilePathData);
		eglSourceFilePath.addModifyListener(this);
		eglSourceFilePath.setEnabled(false);
		
		browseForSourceFile = new Button(sourceFileContainer, SWT.NONE);
		browseForSourceFile.setText("Browse Workspace...");
		browseForSourceFile.addListener(SWT.Selection, new SelectSourceListener(eglSourceFilePath));
		browseForSourceFile.setEnabled(false);
		
// EGL Output:		
		Group eglGroup = new Group(control, SWT.SHADOW_ETCHED_IN);
		eglGroup.setLayout(new GridLayout(1, false));
		eglGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		eglGroup.setText("LaTeX output: ");			
		
		Composite outputFileContainer = new Composite(eglGroup, SWT.NONE);
		outputFileContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		outputFileContainer.setLayout(new GridLayout(2, false));
		
		GridData outputFilePathData = new GridData(GridData.FILL_HORIZONTAL);
		outputFilePathData.horizontalIndent = 25;
		eglOutputFilePath = new Text(outputFileContainer, SWT.BORDER);
		eglOutputFilePath.setLayoutData(outputFilePathData);
		eglOutputFilePath.addModifyListener(this);
		
		browseForOutputFile = new Button(outputFileContainer, SWT.NONE);
		browseForOutputFile.setText("Browse Workspace...");
		browseForOutputFile.addListener(SWT.Selection, new SelectSourceListener(eglOutputFilePath));
		browseForOutputFile.setEnabled(true);
		
		control.layout();
		control.pack();
		
		canSave();
	}
	
	@Override
	public boolean canSave(){
//		TODO want to validate the text box fields.
		return true;
	}
	
	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
		super.setDefaults(configuration);
		
		configuration.setAttribute(MODEL, "");
		configuration.setAttribute(OUTPUT_FILE_PATH, "");
		configuration.setAttribute(USE_DEFAULT_TEMPLATES, true);
		configuration.setAttribute(SOURCE, ""); //TODO: set to  <PROJ>/Template/Logic.egl 
	}
	
	public void initializeFrom(ILaunchConfiguration configuration) {
		super.initializeFrom(configuration);
		
		try {
			useDefaultTemplates.setSelection(configuration.getAttribute(USE_DEFAULT_TEMPLATES, true));
			
			if (!useDefaultTemplates.getSelection()){
				eglSourceFilePath.setEnabled(true);
				browseForSourceFile.setEnabled(true);
			}
			
			eglSourceFilePath.setText(configuration.getAttribute(SOURCE, "")); //TODO: set to <PROJ>/Templates/Logic.egl
			eglOutputFilePath.setText(configuration.getAttribute(OUTPUT_FILE_PATH, ""));			
		} catch (CoreException e) {
			// ignore
		}
		
	}

	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
		configuration.setAttribute(USE_DEFAULT_TEMPLATES, useDefaultTemplates.getSelection());
		configuration.setAttribute(SOURCE, eglSourceFilePath.getText());
		configuration.setAttribute(OUTPUT_FILE_PATH, eglOutputFilePath.getText());
	}
	
	@Override
	public CZTPlugin getPlugin() {
		return CZTPlugin.getDefault();
	}

	@Override
	public String getImagePath() {
		return "icons/AUtoZLogo.png";
	}

	@Override
	public String getFileExtension() {
		return "ftl";
	}

	@Override
	public String getSelectionTitle() {
		return "Select an FTL Program";
	}

	@Override
	public String getSelectionSubtitle() {
		return "FTL Programs in Workspace";
	}

	@Override
	public String getTitle() {
		return "Output";
	}

	public void widgetDefaultSelected(SelectionEvent e) {
		System.out.println("widgetDefaultSelected");
	}

	public void widgetSelected(SelectionEvent e) {
		canSave();
		updateLaunchConfigurationDialog();
	}
	
}
